<?php

class IndexController extends Controller{
    private $db;
    private $qdb;
    public function __init()
    {
    	$this->db=K('category');
        $this->qdb=K('question');
        //获得全部栏目
        $category=$this->db->getList();
        //给栏目加class
        $gcid=Q('cid',0,'intval');
        $cname='';
        foreach ($category as $k => $v) {
            if ($v['cid']==$gcid) {
                $category[$k]['class']="class='a1'";
                $cname=$v['catname'];
            }
        }
        $this->assign('cname',$cname);
        $this->assign('cat',$category);
        //热门问题
        $hot=$this->qdb->order("qclick desc")->limit(4)->all();
        $this->assign('hot',$hot);
        //今日新人榜
        $newpeople=M('user')->order('utime desc')->limit(10)->all();
        foreach ($newpeople as $k => $v) {
            $newpeople[$k]['xuhao']=$k+1;
        }
        $this->assign('newpeople',$newpeople);

    }
    //首页展示
    public function index(){
        //获得所有问题 
        $question=$this->qdb->getList();
        $this->assign($question);
        $this->display();     
    }


    //显示对应栏目的问题
    public function catque()
    {
        $cid=Q('cid',0,'intval');
        
        $data=$this->qdb->where("question.cid=$cid")->order("qtime desc")->all();
        P($data);
        //一问一答
        $q_a=end($data);
        $q=$q_a['qid'];
        $ans=M('answer')->field('answer')->where("qid=$q")->find();
        $ans['answer']=isset($ans['answer'])?mb_substr($ans['answer'],0,50,'utf-8'):'期待你的回答！';
        $ywyd=array('q'=>$q_a['qname'],'a'=>$ans['answer'],'qid'=>$q_a['qid']);
        $this->assign('ywyd',$ywyd);
       
        $this->assign('data',$data);
        $this->display('index.html');    
    }


    //显示答案详情
    public function show()
    {
        //获得当前的qid
        $qid=Q('qid');
        //获得问题
        $question=K('Question')->where(" qid=$qid")->find();
        if (isset($question['accept']) && $question['accept']==1) {
            $answer=K('Answer')->where("qid=$qid")->find();
             $this->assign(array('que'=>$question,'ans'=>$answer));
            $this->display('accept.html');
        }else{
        //获得答案
        $answer=K('Answer')->where(" qid=$qid")->order(" atime desc")->all();
        $this->assign(array('que'=>$question,'ans'=>$answer));
        $this->display();
        }
    }

    //验证码区块
    public function code()
    {
        $code = new code(200,30,'#dcdcdc','#f00',4,22);
        $code->show();
    }
    //退出登录
    public function out()
    {
       session_unset($_SESSION);
       session_destroy();
       $this->success('退出成功','index');
    }

    //答案搜索
    public function search()
    {
       $txt=Q('txt');
       if(!$txt)$this->error('搜索内容不存在');
       $data=K('Question')->where('qname like "%'.$txt.'%"')->all();
        foreach ($data as $id=>$v) {
           $content=M('answer')->where("qid='{$v['qid']}'")->find();
           $data[$id]['con']=isset($content['answer'])?$content['answer']:'期待你的回答';
        }
        $this->assign('data',$data);
       $this->display();
    }

    //采纳回答
    public function accept()
    {
        $aid=Q('aid',0,'intval');
        $qid=Q('qid');
        $que=K('question')->find($qid);
        if(!isset($_SESSION['uid'])){
            $this->error('请登陆后再采纳');
            return false;
        }
        if ($que['uid']!=$_SESSION['uid']) {
            $this->error('该问题为其他人的提问,采纳失败');
            return false;
        }
        //被采纳的问题得accept设置为1
        M('question')->where("qid=$qid")->save(array('accept'=>1));
        $answer=k('answer')->find($aid);
        //删除未采纳的答案
        K('answer')->where("qid=$qid and aid!=$aid")->del();
        $this->assign(array('que'=>$que,'ans'=>$answer));
        $this->display();
    }

    //我的提问
    public function myque()
    {
        $uid=isset($_SESSION['uid'])?$_SESSION['uid']:0;
        if ($uid==0) {
           go('index');
        }
        $question=M('question')->where(" uid=$uid")->all();
        $this->assign('que',$question);
        $this->display();
    }

    //点赞处理
    public function zhan()
    {
       $aid=$_POST['aid'];
       $data=M('answer')->find($aid);
       $click=$data['aclick']+1;
       return M('answer')->where("aid=$aid")->save(array('aclick'=>$click));
    }

    //个人信息
    public function info()
    {
        $uid=Q('uid',0,'intval');
        $info=K('user')->find($uid);
        unset($info['uid']);
        unset($info['password']);
        $que=M('question')->where(" uid=$uid")->all();
        foreach ($que as $k => $v) {
            $que[$k]['qname']=mb_substr($v['qname'],0,18,'utf-8');
        }
        $this->assign(array('info'=>$info,'que'=>$que));
        $this->display();
    }


}
